﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace ud_Video.Business
{
    public class DBVideoClip
    {
        readonly VideoClipDC db = new VideoClipDC();

        public ud_VideoClip GetOne(Guid itemGuid)
        {
            var result = from p in db.ud_VideoClips
                         where p.Guid.Equals(itemGuid)
                         select p;
            return result.FirstOrDefault();
        }

        public ud_VideoClip GetLastest(int moduleID)
        {
            var result = from p in db.ud_VideoClips
                         where p.IsPublished.Equals(true) && p.ModuleID.Equals(moduleID)
                         orderby p.LastModifiedDate descending
                         select p;
            return result.FirstOrDefault();
        }

        public List<ud_VideoClip> GetAll(int moduleID)
        {
            var result = from p in db.ud_VideoClips
                         where p.ModuleID.Equals(moduleID)
                         orderby p.LastModifiedDate descending
                         select p;
            return result.ToList();
        }

        public List<ud_VideoClip> GetVideos(int moduleID, int number)
        {
            var result = from p in db.ud_VideoClips
                         where p.ModuleID.Equals(moduleID)
                         orderby p.LastModifiedDate descending
                         select p;
            if(result.Count() > number)
            {
                result.Take(number);
            }
            return result.ToList();
        }

        public void Delete(Guid itemGuid)
        {
            var result = from p in db.ud_VideoClips
                         where p.Guid.Equals(itemGuid)
                         select p;
            db.ud_VideoClips.DeleteOnSubmit(result.FirstOrDefault());
            db.SubmitChanges();
        }

        public bool Create(int moduleID, string name, string videoURL, string imageURL, string description, bool isPublished)
        {
            bool flag = false;
            ud_VideoClip item = new ud_VideoClip
                                    {
                                        Guid = Guid.NewGuid(),
                                        ModuleID = moduleID,
                                        Name = name,
                                        VideoURL = videoURL,
                                        ImageURL =  imageURL,
                                        Description = description,
                                        IsPublished = isPublished,
                                        CreatedDate = DateTime.UtcNow,
                                        LastModifiedDate = DateTime.UtcNow
                                    };
            db.ud_VideoClips.InsertOnSubmit(item);
            try
            {
                db.SubmitChanges();
                flag = true;
            }
            catch { }
            return flag;
        }

        public bool Update(Guid itemGuid, string name, string videoURL, string imageURL, string description, bool isPublished)
        {
            bool flag = false;
            ud_VideoClip item = GetOne(itemGuid);
            item.Name = name;
            item.VideoURL = videoURL;
            item.ImageURL = imageURL;
            item.Description = description;
            item.IsPublished = isPublished;
            item.LastModifiedDate = DateTime.UtcNow;
            try
            {
                db.SubmitChanges();
                flag = true;
            }
            catch { }
            return flag;
        }
    }
}
